package cn.edu.zju.ops.processors;

import cn.edu.zju.ops.service.ResetService;
import com.alibaba.fastjson.JSONObject;
import com.github.kfcfans.powerjob.worker.core.processor.ProcessResult;
import com.github.kfcfans.powerjob.worker.core.processor.TaskContext;
import com.github.kfcfans.powerjob.worker.core.processor.sdk.BasicProcessor;
import com.github.kfcfans.powerjob.worker.log.OmsLogger;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 重置在线试用平台的数据
 *
 * @author tjq
 * @since 2020/6/29
 */
@Component
public class ResetProcessor implements BasicProcessor {

    @Resource
    private ResetService resetService;

    @Override
    public ProcessResult process(TaskContext context) throws Exception {

        OmsLogger omsLogger = context.getOmsLogger();
        try {
            resetService.resetAppName();
            resetService.resetJobs();
            resetService.resetWorkflow();

            JSONObject jsonObject = JSONObject.parseObject(context.getJobParams());
            resetService.resetContainer(jsonObject.getString("containerVersion"));
        }catch (Exception e) {
            omsLogger.error("process failed.", e);
            return new ProcessResult(false, "failed");
        }

        return new ProcessResult(true, "reset successfully");
    }
}
